﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using HugeFlow.CommandPattern;

namespace HugeFlow
{
    public class MainPageViewModel : ViewModelBase
    {

        #region Properties

        #region Favorites

        private ObservableCollection<FlickrPhoto> _favorites;
        /// <summary>
        /// 즐겨찾기 이미지들
        /// </summary>
        public ObservableCollection<FlickrPhoto> Favorites
        {
            get
            {
                return _favorites;
            }
            set
            {
                _favorites = value;

                OnPropertyChanged("Favorites");
            }
        }

        #endregion Favorites


        #region CurrentPhoto

        private FlickrPhoto _currentPhoto;
        /// <summary>
        /// 현재보고 있는 사진
        /// </summary>
        public FlickrPhoto CurrentPhoto
        {
            get
            {
                return _currentPhoto;
            }
            set
            {
                _currentPhoto = value;

                OnPropertyChanged("CurrentPhoto");
            }
        }

        #endregion CurrentPhoto


        #endregion Properties


        #region Commands

        public static StaticCommand AddFavoriteItem { get; set; }

        public static StaticCommand ShowLargeImage { get; set; }

                /// <summary>
        /// DeleteItem InstantCommand for ViewModel
        /// </summary>
        private InstantCommand<object> _DeleteItemCommand;
        public InstantCommand<object> DeleteItemCommand
        {
            get
            {
                return _DeleteItemCommand;
            }
            set
            {
                _DeleteItemCommand = value;
                OnPropertyChanged("DeleteItemCommand");
            }
        }

        private void DeleteItemCommandExecute(object parameter)
        {
            // code here
            if (Favorites == null)
                return;

            Favorites.Remove(parameter as FlickrPhoto);
        }        
        
        #endregion Commands



        #region Constructors

        public MainPageViewModel()
        {
            Favorites = new ObservableCollection<FlickrPhoto>();

            // Command

            DeleteItemCommand = new InstantCommand<object>(DeleteItemCommandExecute);

            AddFavoriteItem = new StaticCommand("AddFavoriteItem");
            AddFavoriteItem.Executed += new EventHandler<ExecutedEventArgs>(AddFavoriteItem_Executed);

            ShowLargeImage = new StaticCommand("ShowLargeImage");
            ShowLargeImage.Executed += new EventHandler<ExecutedEventArgs>(ShowLargeImage_Executed);

            // Command
        }

        #endregion Constructors

        #region Command Executed

        void ShowLargeImage_Executed(object sender, ExecutedEventArgs e)
        {
            FlickrPhoto photo = e.Parameter as FlickrPhoto;
            if (photo == null)
                return;

            CurrentPhoto = photo;
        }

        void AddFavoriteItem_Executed(object sender, ExecutedEventArgs e)
        {
            FlickrPhoto photo = e.Parameter as FlickrPhoto;
            if (photo == null)
                return;

            Favorites.Add(photo);
        }
        
        #endregion Command Executed
    }
}
